<template>
  <header class="app-header">
    <div class="header-container">
      <!-- 顶部信息栏 -->
      <div class="header-top">
        <div class="top-left">
          <span class="welcome-text">欢迎来到AI Shop</span>
        </div>
        <div class="top-right">
          <div class="top-nav">
            <router-link to="/user/order" class="top-nav-item">我的订单</router-link>
            <router-link to="/user/favorites" class="top-nav-item">我的收藏</router-link>
            <router-link to="/user/coupons" class="top-nav-item">优惠券</router-link>
            <a href="#" class="top-nav-item">客户服务</a>
          </div>
        </div>
      </div>
      
      <!-- 主要导航栏 -->
      <div class="header-main">
        <!-- Logo -->
        <div class="logo-container">
          <router-link to="/" class="logo">
            <h1>AI Shop</h1>
          </router-link>
        </div>
        
        <!-- 导航菜单 -->
        <nav class="main-nav">
          <router-link to="/" class="nav-item">首页</router-link>
          <router-link to="/category" class="nav-item">全部分类</router-link>
          <router-link to="/product/list?recommend=hot" class="nav-item">热门推荐</router-link>
          <router-link to="/product/list?recommend=new" class="nav-item">新品上市</router-link>
          <router-link to="/product/list?recommend=discount" class="nav-item">限时折扣</router-link>
        </nav>
        
        <!-- 搜索框 -->
        <div class="search-container">
          <div class="search-box">
            <input 
              type="text" 
              v-model="searchKeyword" 
              placeholder="搜索商品"
              @keyup.enter="handleSearch"
              class="search-input"
            />
            <button @click="handleSearch" class="search-btn">
              <el-icon><Search /></el-icon>
            </button>
          </div>
        </div>
        
        <!-- 用户和购物车 -->
        <div class="user-cart-container">
          <!-- 用户信息 -->
          <div class="user-info" v-if="userStore.isLogin">
            <el-dropdown placement="bottom">
              <span class="user-trigger">
                <el-avatar :size="40" :src="userStore.userInfo.avatar || '/default-avatar.png'"></el-avatar>
                <span class="user-name">{{ userStore.userInfo.username || '用户中心' }}</span>
                <el-icon class="el-icon--right"><ArrowDown /></el-icon>
              </span>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item>
                    <router-link to="/user/profile" class="dropdown-link">个人中心</router-link>
                  </el-dropdown-item>
                  <el-dropdown-item>
                    <router-link to="/user/address" class="dropdown-link">收货地址</router-link>
                  </el-dropdown-item>
                  <el-dropdown-item>
                    <router-link to="/user/order" class="dropdown-link">我的订单</router-link>
                  </el-dropdown-item>
                  <el-dropdown-item>
                    <router-link to="/user/favorites" class="dropdown-link">我的收藏</router-link>
                  </el-dropdown-item>
                  <el-dropdown-item divided @click="handleLogout">
                    <span class="logout-text">退出登录</span>
                  </el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
          
          <!-- 未登录状态 -->
          <div v-else class="login-register">
            <router-link to="/login" class="login-btn">登录</router-link>
            <router-link to="/register" class="register-btn">注册</router-link>
          </div>
          
          <!-- 购物车 -->
          <div class="cart-info">
            <router-link to="/cart" class="cart-btn">
              <el-icon class="cart-icon"><ShoppingCart /></el-icon>
              <span class="cart-text">购物车</span>
              <span v-if="cartStore.cartItems.length > 0" class="cart-count">
                {{ cartStore.cartItems.length }}
              </span>
            </router-link>
          </div>
        </div>
      </div>
    </div>
    
    <!-- 移动端菜单 -->
    <div class="mobile-menu" v-if="isMobileMenuOpen">
      <div class="mobile-menu-content">
        <div class="mobile-search">
          <input 
            type="text" 
            v-model="searchKeyword" 
            placeholder="搜索商品"
            @keyup.enter="handleSearch"
            class="mobile-search-input"
          />
          <button @click="handleSearch" class="mobile-search-btn">
            <el-icon><Search /></el-icon>
          </button>
        </div>
        
        <div class="mobile-nav">
          <router-link to="/" class="mobile-nav-item" @click="toggleMobileMenu">首页</router-link>
          <router-link to="/category" class="mobile-nav-item" @click="toggleMobileMenu">全部分类</router-link>
          <router-link to="/product/list?recommend=hot" class="mobile-nav-item" @click="toggleMobileMenu">热门推荐</router-link>
          <router-link to="/product/list?recommend=new" class="mobile-nav-item" @click="toggleMobileMenu">新品上市</router-link>
          <router-link to="/product/list?recommend=discount" class="mobile-nav-item" @click="toggleMobileMenu">限时折扣</router-link>
          
          <div class="mobile-user-links">
            <router-link to="/user/order" class="mobile-nav-item" @click="toggleMobileMenu">我的订单</router-link>
            <router-link to="/user/favorites" class="mobile-nav-item" @click="toggleMobileMenu">我的收藏</router-link>
            <router-link to="/user/coupons" class="mobile-nav-item" @click="toggleMobileMenu">优惠券</router-link>
          </div>
          
          <div class="mobile-auth" v-if="!userStore.isLogin">
            <router-link to="/login" class="mobile-login-btn" @click="toggleMobileMenu">登录</router-link>
            <router-link to="/register" class="mobile-register-btn" @click="toggleMobileMenu">注册</router-link>
          </div>
          
          <div v-else class="mobile-user-info">
            <div class="user-profile">
              <el-avatar :size="60" :src="userStore.userInfo.avatar || '/default-avatar.png'"></el-avatar>
              <span class="user-name">{{ userStore.userInfo.username || '用户中心' }}</span>
            </div>
            <div class="mobile-user-menu">
              <router-link to="/user/profile" class="mobile-nav-item" @click="toggleMobileMenu">个人中心</router-link>
              <router-link to="/user/address" class="mobile-nav-item" @click="toggleMobileMenu">收货地址</router-link>
              <button class="mobile-logout-btn" @click="handleLogout">退出登录</button>
            </div>
          </div>
        </div>
      </div>
    </div>
    
    <!-- 移动端菜单遮罩 -->
    <div 
      v-if="isMobileMenuOpen" 
      class="mobile-menu-mask" 
      @click="toggleMobileMenu"
    ></div>
  </header>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '../../store/modules/user'
import { useCartStore } from '../../store/modules/cart'
import { isMobile } from '../../utils/helper'
import { Search, ShoppingCart, ArrowDown } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'

const router = useRouter()
const userStore = useUserStore()
const cartStore = useCartStore()
const searchKeyword = ref('')
const isMobileMenuOpen = ref(false)
const isMobileDevice = computed(() => isMobile())

// 处理搜索
const handleSearch = () => {
  if (searchKeyword.value.trim()) {
    router.push({ path: '/search', query: { keyword: searchKeyword.value.trim() } })
    searchKeyword.value = ''
    toggleMobileMenu(false)
  } else {
    ElMessage.warning('请输入搜索关键词')
  }
}

// 处理退出登录
const handleLogout = () => {
  userStore.logout()
  ElMessage.success('退出登录成功')
  router.push('/login')
  toggleMobileMenu(false)
}

// 切换移动端菜单
const toggleMobileMenu = (open) => {
  if (typeof open === 'boolean') {
    isMobileMenuOpen.value = open
  } else {
    isMobileMenuOpen.value = !isMobileMenuOpen.value
  }
}

// 监听屏幕尺寸变化
const handleResize = () => {
  if (window.innerWidth >= 768 && isMobileMenuOpen.value) {
    isMobileMenuOpen.value = false
  }
}

// 添加窗口尺寸变化监听
window.addEventListener('resize', handleResize)
</script>

<style scoped>
.app-header {
  background-color: #fff;
  border-bottom: 1px solid $border-color;
  position: sticky;
  top: 0;
  z-index: 1000;
}

.header-container {
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  box-sizing: border-box;
}

/* 顶部信息栏 */
.header-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 40px;
  background-color: #f5f5f5;
  border-bottom: 1px solid $border-color;
}

.welcome-text {
  color: $text-color-secondary;
  font-size: $font-size-sm;
}

.top-nav {
  display: flex;
  gap: 20px;
}

.top-nav-item {
  color: $text-color-secondary;
  font-size: $font-size-sm;
  transition: color 0.3s;
}

.top-nav-item:hover {
  color: $primary-color;
}

/* 主要导航栏 */
.header-main {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 80px;
  position: relative;
}

/* Logo */
.logo-container .logo {
  color: $primary-color;
  font-size: 28px;
  font-weight: bold;
  text-decoration: none;
  transition: color 0.3s;
}

.logo-container .logo:hover {
  color: $primary-color-dark;
}

/* 导航菜单 */
.main-nav {
  display: flex;
  gap: 30px;
}

.nav-item {
  color: $text-color;
  font-size: $font-size-base;
  font-weight: 500;
  text-decoration: none;
  padding: 8px 0;
  position: relative;
  transition: color 0.3s;
}

.nav-item:hover {
  color: $primary-color;
}

.nav-item.active {
  color: $primary-color;
}

.nav-item.active::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 2px;
  background-color: $primary-color;
}

/* 搜索框 */
.search-container {
  flex: 0 0 300px;
  margin: 0 20px;
}

.search-box {
  display: flex;
  align-items: center;
  height: 40px;
  border: 1px solid $border-color;
  border-radius: $border-radius-base;
  background-color: #fff;
  overflow: hidden;
}

.search-input {
  flex: 1;
  height: 100%;
  padding: 0 15px;
  border: none;
  outline: none;
  font-size: $font-size-base;
  color: $text-color;
}

.search-input::placeholder {
  color: $text-color-placeholder;
}

.search-btn {
  width: 40px;
  height: 100%;
  border: none;
  background-color: $primary-color;
  color: #fff;
  cursor: pointer;
  transition: background-color 0.3s;
}

.search-btn:hover {
  background-color: $primary-color-dark;
}

/* 用户和购物车 */
.user-cart-container {
  display: flex;
  align-items: center;
  gap: 20px;
}

/* 用户信息 */
.user-info {
  position: relative;
}

.user-trigger {
  display: flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
  padding: 5px 10px;
  border-radius: $border-radius-base;
  transition: background-color 0.3s;
}

.user-trigger:hover {
  background-color: $bg-color;
}

.user-name {
  color: $text-color;
  font-size: $font-size-base;
}

.dropdown-link {
  display: block;
  width: 100%;
  text-align: left;
  color: $text-color;
  text-decoration: none;
}

.logout-text {
  color: #f56c6c;
}

/* 登录注册按钮 */
.login-register {
  display: flex;
  gap: 10px;
}

.login-btn, .register-btn {
  padding: 8px 16px;
  border-radius: $border-radius-base;
  text-decoration: none;
  font-size: $font-size-base;
  transition: all 0.3s;
}

.login-btn {
  color: $text-color;
  background-color: #fff;
  border: 1px solid $border-color;
}

.login-btn:hover {
  color: $primary-color;
  border-color: $primary-color;
}

.register-btn {
  color: #fff;
  background-color: $primary-color;
  border: 1px solid $primary-color;
}

.register-btn:hover {
  background-color: $primary-color-dark;
  border-color: $primary-color-dark;
}

/* 购物车 */
.cart-info {
  position: relative;
}

.cart-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
  border-radius: $border-radius-base;
  color: $text-color;
  text-decoration: none;
  background-color: #fff;
  border: 1px solid $border-color;
  transition: all 0.3s;
}

.cart-btn:hover {
  color: $primary-color;
  border-color: $primary-color;
  background-color: rgba(59, 130, 246, 0.05);
}

.cart-icon {
  font-size: 20px;
}

.cart-text {
  font-size: $font-size-base;
}

.cart-count {
  position: absolute;
  top: -5px;
  right: -5px;
  min-width: 20px;
  height: 20px;
  padding: 0 6px;
  background-color: $danger-color;
  color: #fff;
  font-size: 12px;
  font-weight: bold;
  line-height: 20px;
  text-align: center;
  border-radius: 10px;
}

/* 移动端菜单 */
.mobile-menu-btn {
  display: none;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border: none;
  background-color: transparent;
  color: $text-color;
  font-size: 24px;
  cursor: pointer;
}

.mobile-menu {
  position: fixed;
  top: 0;
  right: 0;
  width: 280px;
  height: 100vh;
  background-color: #fff;
  box-shadow: -2px 0 10px rgba(0, 0, 0, 0.1);
  z-index: 1001;
  transform: translateX(100%);
  transition: transform 0.3s;
}

.mobile-menu.open {
  transform: translateX(0);
}

.mobile-menu-content {
  height: 100%;
  display: flex;
  flex-direction: column;
  padding: 20px;
}

.mobile-search {
  margin-bottom: 20px;
}

.mobile-search-input {
  width: 100%;
  height: 40px;
  padding: 0 15px;
  border: 1px solid $border-color;
  border-radius: $border-radius-base;
  font-size: $font-size-base;
  outline: none;
}

.mobile-search-btn {
  width: 100%;
  height: 40px;
  margin-top: 10px;
  border: none;
  background-color: $primary-color;
  color: #fff;
  border-radius: $border-radius-base;
  cursor: pointer;
  transition: background-color 0.3s;
}

.mobile-search-btn:hover {
  background-color: $primary-color-dark;
}

.mobile-nav {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.mobile-nav-item {
  padding: 12px 0;
  color: $text-color;
  text-decoration: none;
  font-size: $font-size-base;
  border-bottom: 1px solid $border-color;
}

.mobile-nav-item:hover {
  color: $primary-color;
}

.mobile-user-links {
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid $border-color;
}

.mobile-auth {
  display: flex;
  gap: 10px;
  margin-top: 20px;
}

.mobile-login-btn, .mobile-register-btn {
  flex: 1;
  padding: 10px;
  text-align: center;
  border-radius: $border-radius-base;
  text-decoration: none;
  font-size: $font-size-base;
}

.mobile-login-btn {
  color: $text-color;
  background-color: #fff;
  border: 1px solid $border-color;
}

.mobile-register-btn {
  color: #fff;
  background-color: $primary-color;
  border: 1px solid $primary-color;
}

.mobile-user-info {
  margin-top: 20px;
}

.user-profile {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 20px;
  padding-bottom: 20px;
  border-bottom: 1px solid $border-color;
}

.mobile-user-menu {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.mobile-logout-btn {
  margin-top: 10px;
  padding: 12px 0;
  border: none;
  background-color: transparent;
  color: #f56c6c;
  font-size: $font-size-base;
  cursor: pointer;
  text-align: left;
}

.mobile-menu-mask {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  z-index: 1000;
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .main-nav {
    gap: 20px;
  }
  
  .search-container {
    flex: 0 0 250px;
  }
}

@media (max-width: 992px) {
  .header-top {
    display: none;
  }
  
  .header-main {
    height: 60px;
  }
  
  .main-nav {
    display: none;
  }
  
  .mobile-menu-btn {
    display: flex;
  }
}

@media (max-width: 768px) {
  .header-container {
    padding: 0 10px;
  }
  
  .search-container {
    display: none;
  }
  
  .logo-container .logo h1 {
    font-size: 24px;
  }
  
  .user-cart-container {
    gap: 10px;
  }
  
  .login-btn, .register-btn {
    padding: 6px 12px;
    font-size: $font-size-sm;
  }
  
  .cart-btn {
    padding: 6px 12px;
  }
  
  .cart-text {
    display: none;
  }
}
</style>